﻿Public Partial Class ViewUsers
    Inherits BaseUserControl

    Public Overrides Sub Rebind()

        Try
            Dim membershipUserCollection As MembershipUserCollection = GetMembershipUserCollection(txtUsername.Text, txtEmail.Text, txtComment.Text, txtPasswordQuestion.Text, drpIsOnline.SelectedValue, drpIsApproved.SelectedValue, drpIsLockedOut.SelectedValue, rngCreationDate.FromDate, rngCreationDate.ToDate, rngLastLogin.FromDate, rngLastLogin.ToDate, rngLastActivity.FromDate, rngLastActivity.ToDate, rngLastLockedOut.FromDate, rngLastLockedOut.ToDate, rngLastPasswordChange.FromDate, rngLastPasswordChange.ToDate)
            If membershipUserCollection.Count > 0 Then
                rptUsers.Visible = True
                rptUsers.DataSource = membershipUserCollection
                rptUsers.DataBind()
                Me.Prompt(membershipUserCollection.Count & " user found!", UserControlPromptTypes.Successful)
            Else
                rptUsers.Visible = False
                Me.Prompt("No user found!", UserControlPromptTypes.Successful)
            End If

        Catch ex As Exception
            Me.RaiseError(ex)
        End Try

    End Sub

    Protected Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs)

        If TypeOf sender Is Control AndAlso CType(sender, Control).ID = "btnDelete" Then
            Dim username As String = CType(sender, ImageButton).CommandArgument
            Try
                Dim result As Boolean = Membership.DeleteUser(username)
                If result Then
                    Me.Prompt("User " & username & " deleted successfuly!", UserControlPromptTypes.Successful)
                    Me.NotifyUpdate(sender, e)
                    Rebind()
                Else
                    Me.Prompt("User " & username & " couldn't be deleted!", UserControlPromptTypes.Alert)
                End If

            Catch ex As Exception
                Me.RaiseError(ex)
            End Try
        End If

    End Sub

    Protected Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        Rebind()

    End Sub

    Protected Sub chkActivateRoles_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkActivateRoles.CheckedChanged

        pnlRoles.Visible = chkActivateRoles.Checked
        If chkActivateRoles.Checked Then
            rptRoles.DataSource = Roles.GetAllRoles
            rptRoles.DataBind()
        End If

    End Sub

    Private Function GetMembershipUserCollection(ByVal username As String, ByVal email As String, ByVal comment As String, _
            ByVal passwordQuestion As String, ByVal isOnline As Integer, ByVal isApproved As Integer, ByVal isLockedOut As Integer, _
            ByVal creationDateFrom As Date, ByVal creationDateTo As Date, _
            ByVal lastLoginFrom As Date, ByVal lastLoginTo As Date, _
            ByVal lastActivityFrom As Date, ByVal lastActivityTo As Date, _
            ByVal lastLockedOutFrom As Date, ByVal lastLockedOutTo As Date, _
            ByVal lastPasswordChangeFrom As Date, ByVal lastPasswordChangeTo As Date) _
            As MembershipUserCollection

        Dim outMembershipUserCollection As MembershipUserCollection
        outMembershipUserCollection = Membership.GetAllUsers()

        Dim removalHash As New Hashtable
        Dim validRoles As New Hashtable

        If chkActivateRoles.Checked Then
            For Each dataRow As RepeaterItem In rptRoles.Items
                Dim chkRole As CheckBox = dataRow.FindControl("chkRole")
                If chkRole.Checked Then
                    validRoles.Add(CType(dataRow.FindControl("lblRoleName"), Label).Text, "")
                End If
            Next
        End If

        For Each membershipUser As MembershipUser In outMembershipUserCollection

            If isOnline <> 2 AndAlso membershipUser.IsOnline <> isOnline Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If isApproved <> 2 AndAlso membershipUser.IsApproved <> isApproved Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If isLockedOut <> 2 AndAlso membershipUser.IsLockedOut <> isLockedOut Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If username.Trim.Length > 0 AndAlso membershipUser.UserName.IndexOf(username.Trim) < 0 Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If email.Trim.Length > 0 AndAlso membershipUser.Email.IndexOf(email.Trim) < 0 Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If passwordQuestion.Trim.Length > 0 AndAlso membershipUser.PasswordQuestion.IndexOf(passwordQuestion.Trim) < 0 Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If comment.Trim.Length > 0 AndAlso membershipUser.Comment.IndexOf(comment.Trim) < 0 Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If creationDateFrom > Date.MinValue AndAlso creationDateFrom > membershipUser.CreationDate Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If creationDateTo > Date.MinValue AndAlso creationDateTo < membershipUser.CreationDate Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If lastLoginFrom > Date.MinValue AndAlso lastLoginFrom > membershipUser.LastLoginDate Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If lastLoginTo > Date.MinValue AndAlso lastLoginTo < membershipUser.LastLoginDate Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If lastActivityFrom > Date.MinValue AndAlso lastActivityFrom > membershipUser.LastActivityDate Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If lastActivityTo > Date.MinValue AndAlso lastActivityTo < membershipUser.LastActivityDate Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If lastLockedOutFrom > Date.MinValue AndAlso lastLockedOutFrom > membershipUser.LastLockoutDate Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If lastLockedOutTo > Date.MinValue AndAlso lastLockedOutTo < membershipUser.LastLockoutDate Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If lastPasswordChangeFrom > Date.MinValue AndAlso lastPasswordChangeFrom > membershipUser.LastPasswordChangedDate Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            If lastPasswordChangeTo > Date.MinValue AndAlso lastPasswordChangeTo < membershipUser.LastPasswordChangedDate Then
                removalHash.Add(membershipUser.UserName, "")
                Continue For
            End If

            'Checking for roles
            If chkActivateRoles.Checked Then
                Dim rolesArray() As String = Roles.GetRolesForUser(membershipUser.UserName)
                Dim isInRole As Boolean = False

                If rolesArray.Length = 0 AndAlso chkUsersWithoutRoles.Checked Then
                    isInRole = True
                End If

                For Each role As String In rolesArray
                    If validRoles.ContainsKey(role) Then
                        isInRole = True
                        Exit For
                    End If
                Next

                If Not isInRole Then
                    removalHash.Add(membershipUser.UserName, "")
                    Continue For
                End If

            End If

        Next

        For Each removalUsername As String In removalHash.Keys
            outMembershipUserCollection.Remove(removalUsername)
        Next

        Return outMembershipUserCollection

    End Function

End Class
